<script setup lang="ts">
import { MoreOne } from '@icon-park/vue-next'
import formButton from '@renderer/components/formButton.vue'
import useShareDir from '@renderer/composables/useShareDir'
import helper from '@renderer/utils/helper'
import { Action, ElMessageBox } from 'element-plus'
import { reactive, ref } from 'vue'
const dirDialog = ref(false)
const dialogType = ref('')
const dirForm = reactive({
  name: '',
  editId: 0,
})
const handelDelDir = (item: ShareDirModel) => {
  ElMessageBox.alert('是否要删除该文件夹', '温馨提示', {
    showCancelButton: true,
    callback: async (action: Action) => {
      if (action == 'confirm') {
        await delDir({ dir_id: item.id })
        await getDocDir({ page: 1, pid: 0 })
      }
    },
  })
}
const handleDirName = (item: ShareDirModel) => {
  dialogType.value = 'edit'
  dirForm.name = item.file_name
  dirForm.editId = item.id
  dirDialog.value = true
}
const handleCreateDir = () => {
  dialogType.value = 'create'
  dirForm.name = ''
  dirForm.editId = 0
  dirDialog.value = true
}
const handleSubmit = async () => {
  if (dialogType.value == 'create') {
    await createDocDir({ file_name: dirForm.name, pid: 0 })
  } else if (dialogType.value == 'edit') {
    await updateFileName({ file_name: dirForm.name, file_id: dirForm.editId })
  }
  await getDocDir({ page: shareDirList.value?.current_page, pid: 0 })
  dirDialog.value = false
}
const handleCloseDialog = () => {
  dirForm.name = ''
  dirForm.editId = 0
  dirDialog.value = false
}
const handleNextPage = async (val: number) => {
  await getDocDir({ page: val, pid: 0 })
}
const handleOpenDir = (item: ShareDirModel) => {
  window.api.closeWindow('filePage', 'destroy')
  window.api.openWindow('filePage', { dir_id: item.id, title: item.file_name })
}
const { getDocDir, shareDirList, createDocDir, delDir, updateFileName, queryParams } = useShareDir()
const { checkRule } = helper()
await getDocDir({ page: 1, pid: 0 })
</script>

<template>
  <main
    class="bg-white flex flex-col p-5 gap-6 relative rounded-md overflow-hidden"
    style="height: -webkit-fill-available">
    <div class="flex justify-end" v-if="checkRule('共享文档管理权')">
      <formButton @click="handleCreateDir"> 新建 </formButton>
    </div>
    <div class="grid grid-cols-7 gap-5">
      <div
        class="flex flex-col items-center justify-center gap-4 py-6 hover:bg-[#f5f5f5] hover:rounded-md relative group"
        v-for="(item, index) in shareDirList?.data"
        :key="index">
        <img src="@renderer/public/fileDir.jpg" style="width: 122px; height: 98px" @click="handleOpenDir(item)" />
        <span class="text-black text-base one-line">{{ item.file_name }}</span>
        <el-dropdown
          trigger="click"
          class="bg-white rounded-md absolute right-2 top-2 hidden group-hover:block"
          v-if="checkRule('共享文档管理权')">
          <MoreOne theme="outline" size="24" class="cursor-pointer" fill="#000000" />
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="handleDirName(item)">重命名</el-dropdown-item>
              <el-dropdown-item @click="handelDelDir(item)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
    <el-pagination
      @current-change="handleNextPage"
      :current-page="shareDirList?.current_page"
      :page-size="queryParams.limit"
      :total="shareDirList?.total"
      background
      hide-on-single-page
      class="flex-1 items-end" />
    <el-dialog v-model="dirDialog" width="500" align-center :show-close="false" @close="handleCloseDialog">
      <div class="text-center text-black font-bold text-xl mb-8">
        {{ dialogType == 'edit' ? '修改' : '新建' }}文件夹
      </div>
      <el-form :model="dirForm" inline>
        <el-form-item label="文件夹名称：">
          <el-input v-model="dirForm.name" placeholder="请输入文件夹名称" clearable />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="pt-6 flex gap-10 justify-center">
          <formButton @click="handleCloseDialog"> 取消 </formButton>
          <formButton @click="handleSubmit"> 确定 </formButton>
        </div>
      </template>
    </el-dialog>
  </main>
</template>

<style lang="scss" scoped>
.el-form {
  display: flex;
}
.el-form-item {
  margin-bottom: 0px;
  margin-right: 0px !important;
  flex: 1;
}
.el-dialog__footer {
  padding-top: 0;
}
</style>
